﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="ReadOnlyMatrixElementException.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Exceptions
{
    #region Usings

    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Security;

    using ExulLibrary.Mathematics.LinearAlgebra.Properties;

    #endregion

    /// <summary>
    ///     Describes an error when a code tries to modify a read-only matrix element.
    /// </summary>
    [Serializable]
    public sealed class ReadOnlyMatrixElementException : ArgumentException
    {
        #region Constructors and Destructors

        #region public

        /// <summary>
        ///     Initializes a new instance of the <see cref="ReadOnlyMatrixElementException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="elementRowIndex">
        ///     The row index of element.
        /// </param>
        /// <param name="elementColumnIndex">
        ///     The column index of element.
        /// </param>
        public ReadOnlyMatrixElementException(string paramName, int elementRowIndex, int elementColumnIndex)
            : this(paramName, elementRowIndex, elementColumnIndex, null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="ReadOnlyMatrixElementException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="elementRowIndex">
        ///     The row index of element.
        /// </param>
        /// <param name="elementColumnIndex">
        ///     The column index of element.
        /// </param>
        /// <param name="innerException">
        ///     The inner exception.
        /// </param>
        public ReadOnlyMatrixElementException(
            string paramName, int elementRowIndex, int elementColumnIndex, Exception innerException)
            : base(Resources.ReadOnlyMatrixElement, paramName, innerException)
        {
            this.ElementRowIndex = elementRowIndex;
            this.ElementColumnIndex = elementColumnIndex;
        }

        #endregion

        #region private

        /// <summary>
        ///     Initializes a new instance of the <see cref="ReadOnlyMatrixElementException"/> class.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        private ReadOnlyMatrixElementException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.ElementRowIndex = info.GetInt32("ElementRowIndex");
            this.ElementColumnIndex = info.GetInt32("ElementColumnIndex");
        }

        #endregion

        #endregion

        #region Properties

        #region public

        /// <summary>
        ///     Gets the column index of element.
        /// </summary>
        /// <value>
        ///     The column index of element.
        /// </value>
        public int ElementColumnIndex
        {
            get;
            private set;
        }

        /// <summary>
        ///     Gets the row index of element.
        /// </summary>
        /// <value>
        ///     The row index of element.
        /// </value>
        public int ElementRowIndex
        {
            get;
            private set;
        }

        #endregion

        #endregion

        #region Overridden Properties

        #region public

        /// <summary>
        ///     Gets the error message and the parameter name, or only the error message if no parameter name is set.
        /// </summary>
        /// <value>
        ///     A text string describing the details of the exception.
        /// </value>
        public override string Message
        {
            get
            {
                return string.Format(
                    CultureInfo.CurrentCulture, base.Message, this.ElementRowIndex, this.ElementColumnIndex, this.ParamName);
            }
        }

        #endregion

        #endregion

        #region Overridden Methods

        #region public

        /// <summary>
        ///     Sets the <see cref="SerializationInfo"/> object with the parameter name and additional exception information.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="info"/> is <see langword="null"/>.
        /// </exception>
        [SecurityCritical]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue("ElementRowIndex", this.ElementRowIndex);
            info.AddValue("ElementColumnIndex", this.ElementColumnIndex);
        }

        #endregion

        #endregion
    }
}